package com.hnkywl.sts.service.htdygl;

import com.hnkywl.sts.dao.base.SjzdsyDao;
import com.hnkywl.sts.dao.htdygl.HtysdyDao;
import com.hnkywl.sts.entity.base.Sjzdsy;
import com.hnkywl.sts.entity.htgl.Htys;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-4-14
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class HtysdyService {

    @Autowired
    private HtysdyDao htysdyDao;

    @Autowired
    private SjzdsyDao sjzdsyDao;

    public void setHtysdyDao(HtysdyDao htysdyDao) {
        this.htysdyDao = htysdyDao;
    }

    public void setSjzdsyDao(SjzdsyDao sjzdsyDao) {
        this.sjzdsyDao = sjzdsyDao;
    }

    /**
     * 查询合同元素定义
     *
     * @param htdyId
     * @param htysmc
     * @param pageNo
     * @param pageSize @return
     */
    public String findAllHtysdy(Long htdyId, String htysmc, int pageNo, int pageSize) {
        Page<Htys> htysPage = this.htysdyDao.findHtdyList(htdyId, htysmc, pageNo, pageSize);
        List<Htys> htdyList = htysPage.getResult() == null ? new ArrayList<Htys>() : htysPage.getResult();
        String htysJson = BaseUtils.fetchCollectionToJson(htdyList, "id", "mc", "ysid","dybzd", "yxlx", "qz", "mrz", "wbcd", "bz", "htdy.mc","isread");
        return "{\"total\":" + htysPage.getTotalCount() + ",\"rows\":[" + htysJson + "]}";
    }

    /**
     * 根据合同元素id，查询合同元素信息
     *
     * @param htysdyId
     */
    public Htys findHtysdyById(Long htysdyId) {
        Htys htys = this.htysdyDao.get(htysdyId);
        return htys;

    }

    /**
     * 保存修改的合同元素
     *
     * @param htys
     */
    @Transactional
    public void saveHtysdy(Htys htys) {
        Htys tempHtys = this.htysdyDao.get(htys.getId());
        tempHtys.setBz(htys.getBz());
        tempHtys.setMc(htys.getMc());
        tempHtys.setLastUpdated(new Date());
        tempHtys.setYsid(htys.getYsid());
        tempHtys.setMrz(htys.getMrz());
        tempHtys.setQz(htys.getQz());
        tempHtys.setWbcd(htys.getWbcd());
        tempHtys.setYxlx(htys.getYxlx());
        tempHtys.setIsread(htys.getIsread());
        this.htysdyDao.save(tempHtys);
    }

    /**
     * 获取数据字典
     * @return
     */
    public List<Sjzdsy> findAllSjzdsy() {
         List<Sjzdsy>  sjzdsyList = this.sjzdsyDao.findAll();
        return  sjzdsyList;
    }
}
